#ifndef ARDUINO_HPP
#define ARDUINO_HPP

#include <talos/utils/types.hpp>
#include <talos/utils/Functor.hpp>

namespace talos
{
	class Arduino
	{
		public:
			/*!
			 * \brief constructor
			 * \param serialport /dev/ttyACM*
			 * \param baudrate 9600 by default
			 *
			 * takes the string name of the serial port (e.g. "/dev/ttyACM0")
			 * and a baudrate rate ( bps ) and connects to that port at that speed and 8N1.
			 * opens the port in fully raw mode so you can send binary data.
			 * returns valid fd, or -1 on error
			 */
			Arduino( const std::string serialport, const int baudrate = 9600 );

			/*!
			 * \brief write message
			 * \param message
			 * \return -1 if failed 0 else
			 */
			int writeString( const std::string message );

			/*!
			 * \brief write a byte to serial port
			 * \param b the byte to write
			 * \return -1 if failed 0 else
			 */
			int writeByte( uint8_t b );

			virtual void processMessage( std::string& message ) = 0;

		protected:
			Functor< Arduino > m_listening;        /// Functor which launch the listening thread
			pthread_t          m_listening_thread; /// Listening Thread
			pthread_mutex_t    m_serial_lock;      /// Mutex for the serial port
			int                m_fd_serial;        /// file descriptor of the serial port

			/*!
			 * \brief read message from the arduino in an other thread
			 */
			void* listenSerial( void* unused );
	};
}

#endif // ARDUINO_HPP
